package com.zhb.cloud.service.mail.impl;

import java.io.File;

import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring4.SpringTemplateEngine;

import com.zhb.cloud.core.utils.MailUtil;
import com.zhb.cloud.mail.model.Email;
import com.zhb.cloud.mail.service.MailService;
import com.zhb.cloud.utils.Constant;

/**
 * @ClassName: MailServiceImpl
 * @Description: 邮箱发送实现类
 * @author: zhb zhuhongbin@ehomepay.com.cn
 * @date: 2017年11月6日 下午3:50:15
 *
 */
@Service
public class MailServiceImpl implements MailService {
	private static final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
	@Autowired
	private JavaMailSender mailSender;// 执行者
	@Autowired
	private SpringTemplateEngine templateEngine; // thymeleaf
	/**
	 * 发送者邮箱
	 */
	@Value("${spring.mail.username}")
	public String userName;// 发送者

	@Override
	public void send(Email mail) throws Exception {
		MailUtil mailUtil = new MailUtil();
		logger.info("发送邮件：{}", mail.getContent());
		SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
		simpleMailMessage.setFrom(userName);
		simpleMailMessage.setTo(mail.getEmail());
		simpleMailMessage.setSubject(mail.getSubject());
		simpleMailMessage.setText(mail.getContent());
		mailUtil.start(mailSender, simpleMailMessage);
	}

	@Override
	public void sendHtml(Email mail) throws Exception {
		MailUtil mailUtil = new MailUtil();
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
		helper.setFrom(userName);
		helper.setTo(mail.getEmail());
		helper.setSubject(mail.getSubject());
		helper.setText("<html><body><img src=\"cid:springcloud\" ></body></html>", true);
		// 发送图片
		File file = ResourceUtils.getFile("classpath:static" + Constant.SF_FILE_SEPARATOR + "image"
				+ Constant.SF_FILE_SEPARATOR + "springcloud.png");
		helper.addInline("springcloud", file);
		// 发送附件
		file = ResourceUtils.getFile("classpath:static" + Constant.SF_FILE_SEPARATOR + "file"
				+ Constant.SF_FILE_SEPARATOR + "关注科帮网获取更多源码.zip");
		helper.addAttachment("科帮网", file);
		mailUtil.startHtml(mailSender, message);
	}

	@Override
	public void sendThymeleaf(Email mail) throws Exception {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
		helper.setFrom(userName);
		helper.setTo(mail.getEmail());
		helper.setSubject(mail.getSubject());
		Context context = new Context();
		context.setVariable("email", mail);
		context.setVariable("validateCode", 123);
		String text = templateEngine.process(mail.getTemplate(), context);
		helper.setText(text, true);
		mailSender.send(message);
	}

}
